"""
Case Type   : borrow_memory
Case Name   : 设置enable_borrow_memory无效值，合理报错
Create At   : 2025/4/14
Owner       : @li-xin12345
Description :
    1、alter system set设置enable_borrow_memory为无效值，如-1、200、null、''等
    2、gs_guc set 设置enable_borrow_memory为无效值，如-1、200、null、''等
    3、清理环境
Expect      :
    1、合理报错
    2、合理报错
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BorrowMemoryTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_db_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.param_name = 'enable_borrow_memory'
        self.default_value = self.common.show_param(self.param_name)
        self.param_values = ['-1', '200', 'null', '']

    def test_borrow_memory(self):
        text = ('-----step1:alter system set设置enable_borrow_memory为无效值，'
                '如-1、200、null、''等;expect:合理报错-----')
        self.log.info(text)
        for value in self.param_values:
            res = self.pri_db_sh.execut_db_sql(
                f'ALTER SYSTEM SET {self.param_name} TO \'{value}\';')
            self.log.info(res)
            self.assertIn('ERROR', res, '执行失败：' + text)
            res = self.common.show_param(self.param_name)
            self.log.info(res)
            self.assertEqual(self.default_value, res, '执行失败：' + text)

        text = ('-----step2:gs_guc set 设置enable_borrow_memory为无效值，'
                '如-1、200、null、''等;expect:合理报错-----')
        self.log.info(text)
        for value in self.param_values:
            res = self.pri_db_sh.execute_gsguc(
               'set', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.param_name}={value}', get_detail=True)
            if value == '':
                self.assertIn('gs_guc --help', res, '执行失败：' + text)
            else:
                self.assertIn('ERROR', res, '执行失败：' + text)
            res = self.common.show_param(self.param_name)
            self.log.info(res)
            self.assertEqual(self.default_value, res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step3:清理环境; expect:成功-----'
        self.log.info(text)
        res = self.common.show_param(self.param_name)
        self.log.info(res)
        if res != self.default_value:
            res = self.pri_db_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                f"{self.param_name}='{self.default_value}'")
            self.log.info(res)
            res = self.pri_db_sh.restart_db_cluster()
            self.log.info(res)
            status = self.pri_db_sh.get_db_cluster_status()
            self.assertTrue(res, '执行失败：' + text)
            self.assertTrue("Normal" or "Degrade" in status)
            res = self.common.show_param(self.param_name)
            self.log.info(res)
            self.assertEqual(self.default_value, res, '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')